package crons

import (
	"context"
	"go.didapinche.com/foundation/plat-echo/app/ops/services"
	"go.uber.org/zap"
)

/*
* 通过访问prometheus，不断更新sql的api
 */

type ApiSyncJob struct {
	logger     *zap.Logger
	apiService services.ApiService
}

func NewApiSyncJob(logger *zap.Logger, apiService services.ApiService) *ApiSyncJob {
	job := &ApiSyncJob{
		logger:     logger,
		apiService: apiService,
	}
	return job
}

//定时更新接口级别信息
func (a *ApiSyncJob) Run() {
	a.logger.Info("api info job start")
	ctx := context.Background()
	err := a.apiService.GetApiByApp(ctx)
	if err != nil {
		a.logger.Error("call apiService.GetApiByApp error", zap.Error(err))
	}
	err = a.apiService.GetApiByZeus(ctx)
	if err != nil {
		a.logger.Error("call apiService.GetApiByApp error", zap.Error(err))
	}
	a.logger.Info("api info job end")
}

func (a *ApiSyncJob) Init() {
	a.Run()
}

//每天
func (a *ApiSyncJob) Cron() string {
	return "0 0 9 * * ?"
}

func (a *ApiSyncJob) Name() string {
	return "api_info"
}
